package com.tt.mymu.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tt.mymu.entity.Info;
import com.tt.mymu.entity.User;
import com.tt.mymu.entity.po.InfoPO;
import com.tt.mymu.entity.vo.InfoVOSub;
import com.tt.mymu.mapper.InfoMapper;
import com.tt.mymu.service.InfoService;
import com.tt.mymu.service.UserService;
import com.tt.mymu.service.UserViewService;
import com.tt.mymu.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static java.awt.SystemColor.info;

@Service
public class InfoServiceImpl implements InfoService {
    private static final Logger logger = LoggerFactory.getLogger(InfoServiceImpl.class);
    @Autowired
    private InfoMapper infoMapper;
    @Value("${constant.pool.infoCont}")
    private String infoCount;
    @Value("${constant.pool.infoNew}")
    private String infoNew;

    @Value("${tool.updateFile.img}")
    private String toolUpdateImg;
    @Value("${tool.updateFile.video}")
    private String toolUpdateVideo;
    @Value("${nginx.url}")
    private String nginxUrl;
    @Autowired
    private UserService UserService;
    @Autowired
    private UserViewService userViewService;

    /**
     * 前度展示数据
     *
     * @param type
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result getListInfo(int type, int page, int size) {
        PageHelper.startPage(page, size);
        QueryWrapper<Info> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("auditing", 1); // 只展示通过的
        if (type > 0) {
            if (Integer.valueOf(infoCount) == type) { // 点击量最多
                queryWrapper.orderByDesc("count");
            } else if (Integer.valueOf(infoNew) == type) { // 最新提交
                queryWrapper.orderByDesc("create_time");
            } else {
                queryWrapper.eq("sub_id", type); // 当前状态
            }
        }

        List<Info> infos = infoMapper.selectList(queryWrapper);
        if (infos.size() > 0) {
            PageInfo<Info> pageInfo = new PageInfo(infos);
            return Result.success(pageInfo);
        }

        return Result.build(1, "数据为空");
    }

    @Override
    public Result addInfo(Info info) {
        info.setImg("http://" + info.getImg());
        info.setUrl("http://" + info.getUrl());
        //info.setAuditing(0);
        //info.setStatus(1); // 默认上传状态为收费带有权限
        info.setCreateTime(new Date());
        int insert = infoMapper.insert(info);
        if (insert > 0) {
            return Result.success(insert);
        }
        return Result.build(1, "添加失败");
    }

    @Override
    public Result deleteInfo(Integer id) {
        int i = infoMapper.deleteById(id);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.build(1, "删除失败");
    }

    @Override
    public Result updateInfo(Info info) {

        info.setUpdateTime(new Date());
        int i = infoMapper.updateById(info);
        return Result.success(i);
    }

    @Override
    public Result selectInfoById(Integer id, String cookieValue) {
        Result userByToken = UserService.getUserByToken(cookieValue);
        logger.debug("this token is {}", userByToken);
//        if (userByToken == null) {
//            return Result.build(4011, "用户信息不存在");
//        }
        if (userByToken.getCode() == 1) {
            return Result.build(1, "需要从新登陆");
        }
        //查询视频信息 status 判断视频状态
        Info info = infoMapper.selectById(id);
        // 点击一次，增加点击量
        int count = info.getCount();
        count++;
        info.setCount(count);
        infoMapper.updateById(info);
        if (info != null) {
            if (info.getAuditing() == 0) {
                return Result.build(1, "未审核");
            }
            // 免费的
            if (info.getStatus() == 0) {
                return Result.success(info);
            } else if (info.getStatus() == 1) { // 付费的
                User user = (User) userByToken.getData();
                // 判断视频是否过期
                Result userViewUser = userViewService.getUserViewByUserId(user.getId());
                Integer code = userViewUser.getCode();
                if (1 == code) {
                    return Result.build(2011, "会员已过期");
                }
                return Result.success(info);
            }
        }
        return Result.build(1, "已下架");


    }

    @Override
    public Result getListInfoByVo(InfoPO infoPO, int page, int size) {
        PageHelper.startPage(page, size);

        List<InfoVOSub> infoVOSubs = infoMapper.getListInfoByVo(infoPO);
        PageInfo<Info> pageInfo = new PageInfo(infoVOSubs);
        return Result.success(pageInfo);

    }
}
